package grsu.epam.airline.DAOImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import grsu.epam.airline.DAO.IPlaneDAO;
import grsu.epam.airline.model.Plane;

public class PlaneDAOImpl implements IPlaneDAO {
	private static final String GET_ALL_PLANE_QUERY = "select plane_id, model from plane";
	private static final String UPDATE_PLANE = "update plane set model=? where plane_id=?";
	private static final String ADD_PLANE = "insert into plane (model) values (?)";
	private static final String GET_PLANE_BY_ID_QUERY = "select plane_id,model from plane where plane_id=?";
	private static final String DELETE_PLANE = "DELETE FROM plane WHERE plane_id=?";

	private Connection conn;

	public PlaneDAOImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public void addPlane(Plane plane) throws SQLException {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(ADD_PLANE);
			stmt.setString(1, plane.getModel());
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
	}

	@Override
	public void updatePlane(Plane plane) throws SQLException {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(UPDATE_PLANE);
			stmt.setString(1, plane.getModel());
			stmt.setInt(2, plane.getPlaneId());
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
	}

	@Override
	public Plane getPlaneById(int planeId) throws SQLException {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Plane plane = null;
		try {
			stmt = conn.prepareStatement(GET_PLANE_BY_ID_QUERY);
			stmt.setInt(1, planeId);
			rs = stmt.executeQuery();
			while (rs.next()) {
				plane = new Plane(rs.getInt("plane_id"), rs.getString("model"));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return plane;
	}

	@Override
	public void deletePlane(int planeId) throws SQLException {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(DELETE_PLANE);
			stmt.setInt(1, planeId);
			stmt.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
	}

	@Override
	public List<Plane> getAllPlane() throws SQLException {
		List<Plane> planes = new ArrayList<Plane>();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement(GET_ALL_PLANE_QUERY);
			rs = stmt.executeQuery();
			while (rs.next()) {
				planes.add(new Plane(rs.getInt("plane_id"), rs
						.getString("model")));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return planes;
	}

	@Override
	public Connection getConnection() {
		return this.conn;
	}

}
